package message;

import java.util.Queue;

public class Order {
	private String clientId;
	private String orderId;
	private String symbol;
	private int operation;
	private double price;
	private int qty;
	private int leavesQty;
	private int cumQty;
	private int status;

	public String getClientId() {
		return clientId;
	}

	public String getOrderId() {
		return orderId;
	}

	public double getPrice() {
		return price;
	}

	public int getLeavesQty() {
		return leavesQty;
	}

	public int getQty() {
		return qty;
	}

	public int getOperation() {
		return operation;
	}

	public void fill(int fillQty, double fillPx, Queue<Message> outQueue) {
		// System.out.println("before fill: "+this);
		leavesQty -= fillQty;
		cumQty += fillQty;
		Message message = new Message(this);
		message.setLastPrice(fillPx);
		message.setLastQty(fillQty);
		if (leavesQty > 0)
			message.setType(MessageType.PARTFILL);
		else
			message.setType(MessageType.FILL);
		outQueue.add(message);
		// System.out.println("after fill: "+this);

	}

	public void sendAck(Queue<Message> outQueue) {
		Message message = new Message(this);
		message.setType(MessageType.ACK);
		outQueue.add(message);
		// System.out.println("send ack"+this);
	}

	public Order(String clientId, String orderId, String symbol, int operation,
			double price, int qty) {
		this.orderId = orderId;
		this.clientId = clientId;
		this.symbol = symbol;
		this.operation = operation;
		this.price = price;
		this.qty = qty;
		this.cumQty = 0;
		this.leavesQty = qty;
	}

	@Override
	public String toString() {
		String stat;
		switch (status) {
		case 0:
			stat="new";
			break;
		case 1:
			stat="partfill";
			break;
		case 2:
			stat="filled";
			break;
		case 4:
			stat="cancelled";
			break;
		case 8:
			stat="rejected";
			break;
		default:
			stat="unknown";
			break;
		}		
		return orderId + ":" + ((operation == 1) ? "buy" : "sell") + ":"
				+ price + ":" + leavesQty  +":" + stat ;
	}

	public int getCumQty() {
		return cumQty;
	}

	public String getSymbol() {
		return symbol;
	}

	public void setLeavesQty(int leavesQty) {
		this.leavesQty = leavesQty;
	}

	public void setCumQty(int cumQty) {
		this.cumQty = cumQty;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	
	
}
